package com.quanta.edaily.controller;

import com.quanta.edaily.Service.FavouriteService;
import com.quanta.edaily.bean.JsonResponse;
import com.quanta.edaily.bean.contants.ResultCode;
import com.quanta.edaily.mapper.ArticleMapper;
import com.quanta.edaily.mapper.FavouriteMapper;
import com.quanta.edaily.mapper.UserMapper;
import com.quanta.edaily.pojo.Article;
import com.quanta.edaily.pojo.User;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/favourite")
public class FavouriteController {
    @Resource
    FavouriteService favouriteService;

    @GetMapping("/getArticleByUid/{uid}")
    public JsonResponse getSomeByUid(@PathVariable Integer uid) {
        return JsonResponse.success("查询成功", favouriteService.getSomeByUid(uid));
    }

    @GetMapping( "/getUserByAid/{aid}")
    public JsonResponse getSomeByAid(@PathVariable Integer aid) {
        return JsonResponse.success("查询成功", favouriteService.getSomeByAid(aid));
    }

    @GetMapping("/getOne/{uid}/{aid}")
    public JsonResponse getOne(@PathVariable Integer uid, @PathVariable Integer aid) {
        return JsonResponse.success("查询成功", favouriteService.getOne(uid,aid));
    }

    @PostMapping("/add/{uid}/{aid}")
    public JsonResponse add(@PathVariable Integer uid, @PathVariable Integer aid) {
        if(favouriteService.getOne(uid, aid)!=null) return JsonResponse.success("已收藏");
        if(favouriteService.add(uid,aid)!=1) return JsonResponse.paramError("收藏失败");
        return JsonResponse.success("收藏成功");
    }

    @PostMapping("/cancel/{uid}/{aid}")
    public JsonResponse cancel(@PathVariable Integer uid, @PathVariable Integer aid) {
//        if(favouriteService.getOne(uid, aid)==null) return JsonResponse.success("已取消");
        if(favouriteService.cancel(uid,aid)!=1) return JsonResponse.paramError("收藏失败");
        return JsonResponse.success("取消成功");
    }
}
